package com.example.freeengine.wallpaper.fragment.cyclerefresh;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.example.freeengine.R;
import com.example.freeengine.databinding.FragmentWallpaperCycleRefreshBinding;
import com.example.freeengine.wallpaper.constants.WallPaperUpdateStrategy;
import com.example.freeengine.wallpaper.fragment.WallPaperViewModel;
import com.example.freeengine.wallpaper.fragment.cyclerefresh.albumlist.AlbumListAdapter;
import com.example.freeengine.wallpaper.persistence.entity.WallPaperCycleRefreshAlbum;
import com.example.freeengine.wallpaper.persistence.repository.WallPaperCycleRefreshRepository;

import org.checkerframework.checker.units.qual.A;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Terry Deng
 */
public class WallPaperCycleRefreshFragment extends Fragment {

    private static final String TAG = WallPaperCycleRefreshFragment.class.getSimpleName();
    FragmentWallpaperCycleRefreshBinding viewBinding;
    WallPaperViewModel wallPaperViewModel;
    WallPaperCycleRefreshRepository wallPaperCycleRefreshRepository;

    AlbumListAdapter albumListAdapter;

    public WallPaperCycleRefreshFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        viewBinding = FragmentWallpaperCycleRefreshBinding.inflate(inflater);
        return viewBinding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        wallPaperViewModel = new ViewModelProvider(requireActivity()).get(WallPaperViewModel.class);
        wallPaperCycleRefreshRepository = new WallPaperCycleRefreshRepository(getContext());

        albumListAdapter = new AlbumListAdapter(requireContext(), wallPaperViewModel, wallPaperCycleRefreshRepository, requireActivity());
        viewBinding.recyclerViewWallPaperCycleRefresh.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        viewBinding.recyclerViewWallPaperCycleRefresh.setAdapter(albumListAdapter);
        wallPaperViewModel.getAlbumList(wallPaperCycleRefreshRepository).observe(requireActivity(), new Observer<List<WallPaperCycleRefreshAlbum>>() {
            @Override
            public void onChanged(List<WallPaperCycleRefreshAlbum> wallPaperCycleRefreshAlbums) {

                Log.i(TAG, "change");

                if (wallPaperViewModel.getWallPaperUpdateStrategyInfo(requireContext()).equals(WallPaperUpdateStrategy.FROM_CYCLEREFRESH.name())){
                    albumListAdapter.submitList(wallPaperViewModel.getAlbumInfoList(wallPaperCycleRefreshAlbums, wallPaperCycleRefreshRepository,
                            wallPaperViewModel.getWallPaperUpdateStrategyCycleRefreshInfo(requireContext())));
                }else {
                    albumListAdapter.submitList(wallPaperViewModel.getAlbumInfoList(wallPaperCycleRefreshAlbums, wallPaperCycleRefreshRepository));
                }
            }
        });

        wallPaperViewModel.getCycleRefreshAlbumChange().observe(getViewLifecycleOwner(), new Observer<Long>() {
            @Override
            public void onChanged(Long id) {
                if (id >= 0){
                    List<AlbumInfo> currentList = new ArrayList<>();
                    AlbumInfo newAlbumInfo;
                    for (AlbumInfo albumInfo : albumListAdapter.getCurrentList()){
                        newAlbumInfo = new AlbumInfo();

                        newAlbumInfo.setAlbumId(albumInfo.getAlbumId());
                        newAlbumInfo.setUsing(albumInfo.getAlbumId().equals(id));
                        newAlbumInfo.setPreviewPath(albumInfo.getPreviewPath());
                        newAlbumInfo.setItemCount(albumInfo.getItemCount());
                        newAlbumInfo.setWallPaperCycleRefreshAlbum(albumInfo.getWallPaperCycleRefreshAlbum());

                        currentList.add(newAlbumInfo);
                    }
                    albumListAdapter.submitList(currentList);
                }
            }
        });

        // 添加循环图片集信息
        viewBinding.btnWallpaperCyclerefreshAlbumAdd.setOnClickListener(v -> {
            AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
            LayoutInflater  layoutInflater = LayoutInflater.from(requireActivity());
            View view = layoutInflater.inflate(R.layout.wallpaper_cyclerefresh_album_create, null);

            builder.setView(view);
            Button btnCancel = view.findViewById(R.id.btn_wallpaper_cyclerefresh_album_create_cancel);
            Button btnOk = view.findViewById(R.id.btn_wallpaper_cyclerefresh_album_create_ok);

            EditText albumName = view.findViewById(R.id.et_wallpaper_cyclerefresh_album_create_name);

            AlertDialog dialog = builder.show();

            btnCancel.setOnClickListener(v1 -> {
                dialog.dismiss();
            });

            btnOk.setOnClickListener(v1 -> {

                WallPaperCycleRefreshAlbum wallPaperCycleRefreshAlbum = new WallPaperCycleRefreshAlbum();
                wallPaperCycleRefreshAlbum.setName(albumName.getText().toString());

                try {
                    wallPaperCycleRefreshRepository.insertAlbum(wallPaperCycleRefreshAlbum);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                dialog.dismiss();
            });

        });

    }
}